package com.tedu.bbs.controller;

import com.tedu.bbs.entity.User;
import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.*;

@Controller
public class UserController {
    @RequestMapping("/userList")
    public void  userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理动态页面");
        try {
            Connection connection=DBUtil.getConnection();
            String sql="SELECT id,username,password,nickname,age FROM userinfo ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw=response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>Title</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<body background=\"iii.png\">");
            pw.println("    <h1>用户列表</h1>");
            pw.println("  <table border=\"1\">");
            pw.println("    <tr>");
            pw.println("        <td>ID</td>");
            pw.println("        <td>用户名</td>");
            pw.println("        <td>密码</td>");
            pw.println("        <td>昵称</td>");
            pw.println("        <td>年龄</td>");
            pw.println("    </tr>");
            while (rs.next()){
                int id=rs.getInt("id");
                String username=rs.getString("username");
                pw.println("    <tr>");
                pw.println("        <td>"+id+"</td>");
                pw.println("        <td><a href='/articleList?userID="+id+"'>"+username+"</a></td>");
                pw.println("        <td>"+rs.getString("password")+"</td>");
                pw.println("        <td>"+rs.getString("nickname")+"</td>");
                pw.println("        <td>"+rs.getInt("age")+"</td>");
                pw.println("    </tr>");
            }
            pw.println("  </table>");
            pw.println("    </body>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/regUser")
//    public void reg(HttpServletRequest request, HttpServletResponse response){}

//      public void reg(String username,String password,String nickname,int age,HttpServletResponse response){}
    public void reg(User user,HttpServletResponse response) {
        String username=user.getUsername();
        String password=user.getPassword();
        String nickname=user.getNickname();
        int age=user.getAge();

        if (username==null || username.isEmpty() ||
            password==null || password.isEmpty() ||
            nickname==null || nickname.isEmpty() ){
            try {
                response.sendRedirect("/reg_info_error.html");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (
                Connection connection=DBUtil.getConnection();
        ){

            String sql="SELECT 1 FROM userinfo WHERE username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);


            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("/have_user.html");
                return;
            }
            String sql1="INSERT INTO userinfo (username,password,nickname,age) " +
                    "VALUES (?,?,?,?)";
            PreparedStatement pss=connection.prepareStatement(sql1);
            pss.setString(1,username);
            pss.setString(2,password);
            pss.setString(3,nickname);
            pss.setInt(4,age);
            int num=pss.executeUpdate();
            if (num>0){
                response.sendRedirect("/reg_success.html");
                return;
            }
            pss.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/loginUser")
    public void longin (HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        if (username==null || username.isEmpty() ||
            password==null || password.isEmpty()){

            try {
                response.sendRedirect("login_info_error.html");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            Connection connection=DBUtil.getConnection();

            String sql="SELECT id,username,password,nickname,age "+
                       "FROM userinfo "+
                       "WHERE username=? "+
                       " AND  password=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("login_success.html");
                return;
            }else {
                response.sendRedirect("login_fail.html");
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}












